const tools = {
    isArray(obj) {
        if (typeof Array.isArray === "function") return Array.isArray(obj);
        return Object.prototype.toString.call(obj) === "[object Array]";
    },

    isEmpty(obj) {
        if (obj === undefined) return true;
        if (typeof obj == "string" && obj == "") return true;
        if (typeof obj == "function") return false;
        if (tools.isArray(obj)) return obj.length == 0;
        if (typeof obj == "object") {
            var n = 0;
            for (var p in obj) {
                if (p) {
                    n++;
                }
            }
            return n == 0;
        }
        return false;
    },

    isValidMobile(s) {
        var reg = /^[1][3,4,5,7,8][0-9]{9}$/;
        return reg.test(s);
    },

    // 图片封装
    getImage(uri) {
        return process.env.VUE_APP_IMGFETCH + uri
    },

    // 时间处理
    handleTime(time) {
        let date = new Date(time),
            Y = date.getFullYear(),
            M = date.getMonth() + 1,
            D = date.getDate()
        if (M < 10) {
            M = "0" + M
        }
        if (D < 10) {
            D = "0" + D
        }
        return `${Y}-${M}-${D}`
    },

    // 时间处理
    handleTime(time, type) {
        let date = new Date(time),
            Y = date.getFullYear(),
            M = date.getMonth() + 1,
            D = date.getDate(),
            H = date.getHours(),
            MM = date.getMinutes(),
            S = date.getSeconds()
        if (M < 10) {
            M = "0" + M
        }
        if (D < 10) {
            D = "0" + D
        }
        if (H < 10) {
            H = "0" + H
        }
        if (MM < 10) {
            MM = "0" + MM
        }
        if (S < 10) {
            S = "0" + S
        }
        if (type == 1) {
            return `${Y}-${M}-${D} ${H}:${MM}:${S}`
        } else {
            return `${Y}-${M}-${D}`
        }
    },

    tranFloat(value) {
        var value = Math.round(parseFloat(value) * 100) / 100;
        var xsd = value.toString().split(".");
        if (xsd.length == 1) {
            value = value.toString() + ".0";
            value = Number(value).toFixed(1)
            return value;
        }
        if (xsd.length > 1) {
            if (xsd[1].length < 2) {
                value = value.toString() + "0";
                value = Number(value).toFixed(1)
            }
            return value;
        }
    },

    // 数组去重
    arrDeWeight(arr) {
        let arr2 = [];
        for (var i = 0; i < arr.length; i++) {
            if (arr2.indexOf(arr[i]) < 0) {
                arr2.push(arr[i]);
            }
        }
        return arr2;
    },


    // 小数点处理，不足补零
    getFloat(number, n) {
        n = n ? parseInt(n) : 0;
        if (n <= 0) {
            return Math.round(number);
        }
        number = Math.round(number * Math.pow(10, n)) / Math.pow(10, n); //四舍五入
        number = Number(number).toFixed(n); //补足位数
        return number;
    },

};

export default tools;